package com.xpoint.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.Employee;
import com.xpoint.backend.mapper.EmployeeMapper;
import com.xpoint.backend.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
//    登录到后台
    public R<Employee> login(Employee employee, HttpServletRequest request) {
//        密码加密MD5
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
//        构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        编写sql查询语句，用户名==getUserName
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
//        查询一条数据
        Employee one = getOne(lambdaQueryWrapper);
        if (one == null) {
            return R.error("用户不存在");
        }
        if (!one.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        if (one.getStatus() == 0) {
            return R.error("账号已被禁用");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);
    }

    @Override
//    添加员工
    public R<String> sevaEmp(Employee employee, HttpServletRequest request) {
        if (employee != null) {
//            md5加密password
            String md5DigestAsHex = DigestUtils.md5DigestAsHex("123456".getBytes());
//            设置默认密码
            employee.setPassword(md5DigestAsHex);
            boolean save = save(employee);
            if (save) {
                return R.success("添加成功");
            }
        }
        return R.error("添加失败");
    }

    @Override
//    分页查询Employee表，员工表
    public R<Page> findPage(Long page, Long pageSize, String name) {
//            构造MybatisPlus分页，传入页码及页面大小
        Page page1 = new Page(page, pageSize);
//            构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
//            查询where   like关键字 查询的是Employee映射的表，查询的字段是name
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
//            根据更新日期进行降序排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
//            分页查询
        page(page1, queryWrapper);
//            返回结果集。
        return R.success(page1);
    }

    @Override
//查询根据id查询，响应给前端，前端跳转到编辑页
    public R<Employee> findByid(Long id) {
        if (id != null) {
            Employee byId = getById(id);
            return R.success(byId);
        }
        return R.error("编辑失败");
    }

    @Override
//    更新员工信息
    public R<String> updateEmp(Employee employee, HttpServletRequest request) {
//      根据员工的id进行更新
        updateById(employee);
        if (updateById(employee)) {
            return R.success("成功");
        }
        return R.error("失败");
    }
}
